// pages/superpages/service/service.js
const app = getApp()
const areas = ['所有区域','香洲区','金湾区','斗门区','横琴新区','其他']
Page({
  data: {
    phone: '',
    err: '',
    show:false,
    local: '', 
    servicename:'',
    serviceprople:'',
    adress:'',
    columns: [
      {
        values: areas,
        className: 'column1'
      },
    ]
  },
  //所属区域
  pick() {
    this.setData({
      show: true
    })
  },
  onConfirm(e) {
    this.setData({
      local: e.detail.value.join(''),
      show: false
    })
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
//切换tabbar
  onChange(event) {
    if(event.detail == "2") {
      wx.redirectTo({
        url:'../allservice/allservice'
      })
    } else if (event.detail == "1") {
      wx.redirectTo({
        url: '../purchase/purchase',
      })
    }
  },
  getdressValue(e){
    this.setData({
      adress: e.detail
    })
  },
  getPhoneValue(e) {
    this.setData({
      phone: e.detail
    })
  },
  checkPhone() {
    if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
      this.setData({
        err: '请输入正确的手机号码'
      })
    } else {
      this.setData({
        err: ''
      })
    }
  },
  getserviceValue(e) {
    this.setData({
      servicename: e.detail
    })
  },
  getservicenameValue(e) {
    this.setData({
      serviceprople: e.detail
    })
  },
  // 注册社区服务点
  sign() {
    if (!this.data.serviceprople || !this.data.servicename || !this.data.local || !this.data.phone || !this.data.adress) {
      wx.showToast({
        title: '请完成信息',
        image: '../../../static/icons/err.png',
        duration: 2000,
      })
      return
    }
    var that = this
    let default_address = {
      area:that.data.local,
      service:that.data.servicename,
      local:that.data.adress,
      name:that.data.serviceprople
    }
    wx.login({
      success(res) {
        var model = {
          account_id: that.data.phone,
          password: "123",
          status:"3",
          msg: wx.getStorageSync('userInfo'),
          code: res.code,
          default_address:default_address
        }
        app.api.sign(model, (res) => {
          if (res.statusCode == 401) {
            wx.showToast({
              title: res.data,
              image: '../../../static/icons/err.png',
              duration: 2000
            })
          } else {
            console.log("注册成功")
            wx.showModal({
              title: '注册成功',
              content: '注册账号是：' + that.data.phone +' 默认密码是：123',
              complete(){
                that.setData({
                  servicename:'',
                  serviceprople:'',
                  adress:'',
                  phone:'',
                  local:''
                })
              }
            })
          }
        })
      }
    })
  }
})